<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>订单管理</title>
    <link href="./css/main.css" rel="stylesheet"/>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="./css/element.css"/>
</head>

<body>
<div id="app">
    <el-container>
        <el-header>
            <span class="s1">黑马商城管理系统</span>
            <span class="s2">管理员 <i class="el-icon-switch-button" style="margin-right: 20px"></i></span>
        </el-header>

        <el-container>
            <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
                <el-menu>
                    <el-submenu index="1">
                        <template slot="title"><i class="el-icon-s-promotion"></i>首页</template>
                        <el-menu-item index="1-3"><i class="el-icon-position"></i>工作台</el-menu-item>
                    </el-submenu>

                    <el-submenu index="2">
                        <template slot="title"><i class="el-icon-s-goods"></i>商品管理</template>
                        <el-menu-item index="2-1"><i class="el-icon-magic-stick"></i>分类管理</el-menu-item>
                        <el-menu-item index="2-2" @click="toItem"><i class="el-icon-goods"></i>商品管理</el-menu-item>
                        <el-menu-item index="2-3"><i class="el-icon-s-claim"></i>订单管理</el-menu-item>
                    </el-submenu>

                    <el-submenu index="3">
                        <template slot="title"><i class="el-icon-s-tools"></i>权限管理</template>
                        <el-menu-item index="3-1"><i class="el-icon-user"></i>用户管理</el-menu-item>
                        <el-menu-item index="3-2"><i class="el-icon-goods"></i>角色管理</el-menu-item>
                        <el-menu-item index="3-3"><i class="el-icon-pie-chart"></i>菜单管理</el-menu-item>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <el-table :data="orders" border style="width: 100%">
                    <el-table-column prop="id" label="编号" width="120" align="center"></el-table-column>
                    <el-table-column prop="userId" label="用户ID" width="120" align="center"></el-table-column>
                    <el-table-column prop="totalFee" label="订单金额" width="100" align="center">
                        <template slot-scope="scope"> ￥{{scope.row.totalFee}}</template>
                    </el-table-column>
                    <el-table-column prop="paymentType" width="120" label="付款方式" align="center">
                        <template slot-scope="scope">
                            {{scope.row.paymentType==1?'微信支付':scope.row.paymentType==2?'支付宝支付':'扣减余额'}}
                        </template>
                    </el-table-column>
                    <el-table-column prop="status" label="订单状态" width="120" align="center">
                        <template slot-scope="scope">
                            <div :style="{color:(scope.row.status?'green':'red')}">
                                {{scope.row.status?'已完成':'未支付'}}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createTime" label="下单时间" width="120" align="center"
                                     show-overflow-tooltip></el-table-column>
                    <el-table-column prop="consignTime" label="发货时间" width="120" align="center"
                                     show-overflow-tooltip></el-table-column>
                    <el-table-column prop="endTime" label="收货时间" width="120" align="center"
                                     show-overflow-tooltip></el-table-column>
                    <el-table-column align="center" label="操作" :width="160">
                        <template slot-scope="scope">
                            <el-button type="primary" plain icon="el-icon-edit" circle size="mini"
                                       @click="handleEdit(scope.$index, scope.row)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <br/>
                <!-- 分页条 -->
              <el-pagination @current-change="query" style="margin-top: 5px" :page-size="10"
                             layout="prev, pager, next" :total="total"></el-pagination>
            </el-main>
        </el-container>
    </el-container>

    <!-- 新增、编辑对话框 -->
    <el-dialog title="订单信息" :visible.sync="formVisible" width="50%" style="padding: 0 20px">
        <el-form :model="order" :inline="true" size="small" label-position="left" :label-width="formLabelWidth">
            <el-form-item label="订单金额">
                <el-input v-model="order.totalFee" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="付款方式">
                <el-select v-model="order.paymentType" placeholder="请选择付款方式">
                    <el-option v-for="item in paymentTypes" :key="item.value" :label="item.label"
                               :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="订单状态">
                <el-select v-model="order.status" placeholder="请选择订单状态">
                    <el-option v-for="item in statuses" :key="item.value" :label="item.label"
                               :value="item.value"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="formVisible = false" size="medium">取 消</el-button>
            <el-button type="primary" @click="confirmEdit" size="medium">确 定</el-button>
        </div>
    </el-dialog>
</div>

<script src="./js/vue.js"></script>
<script src="./js/axios.min.js"></script>
<!-- 引入组件库 -->
<script src="./js/element.js"></script>
<script>
    // 设置后台服务地址
    axios.defaults.baseURL = 'http://localhost:10010'
    axios.defaults.timeout = 3000

    const app = new Vue({
        el: '#app',
        data: {
            orders: [],
            order: {},
            total: 1000,
            formVisible: false, // 是否显示表单
            formLabelWidth: '100px', // 表单label宽度
            item: {
                isAD: false,
                image: '',
            }, // 表单中的酒店数据
            isEdit: false, // 是否是更新
            lastPage: 1, // 上一次查询的页码
            paymentTypes: [
                {
                    value: 1,
                    label: '支付宝',
                },
                {
                    value: 2,
                    label: '微信',
                },
                {
                    value: 3,
                    label: '扣减金额',
                },
            ],
            statuses: [
                {
                    value: 0,
                    label: '待支付',
                },
                {
                    value: 1,
                    label: '已支付',
                },
            ],
        },

        created() {
            this.query(1)
        },

        methods: {
            toItem() {
                location.href = 'index.html'
            },
            query(page) {
                this.lastPage = page
                axios
                    .get('/order', {
                        params: {
                            page: page,
                            size: 10,
                        },
                    })
                    .then((resp) => {
                        this.orders = resp.data.records
                        this.total = resp.data.total
                    })
                    .catch((err) => console.log(err))
            },
            handleEdit(v1, v2) {
                this.isEdit = true
                this.formVisible = true
                this.order = v2
            },

            confirmEdit() {
                if (this.isEdit) {
                    axios
                        .put('/order', this.order)
                        .then((resp) => {
                            this.$message({
                                message: '更新成功',
                                type: 'success',
                            })
                            this.formVisible = false
                            this.reload()
                        })
                        .catch((err) => {
                            this.$message({
                                message: '更新失败',
                                type: 'error',
                            })
                            console.log(err)
                        })
                }
            },

            handleUp(id) {
                this.updateStatus(id, 1)
            },
            handleDown(id) {
                this.updateStatus(id, 2)
            },

            reload() {
                this.query(this.lastPage)
            },

            parseJSON(str) {
                return JSON.parse(str)
            },
        },
    })
</script>
</body>
</html>
